package com.avic.rong.course.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.course.adapter.PeiXunLineClassRvAdapter;
import com.avic.rong.course.adapter.PeiXunCourseVideoRvAdapter;
import com.avic.rong.course.bean.PeiXunListBean;
import com.avic.rong.course.showapi.PeiXunListApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PeiXunLineClassActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {


    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.iv_right)
    ImageView ivRight;

    @BindView(R.id.ll_root)
    LinearLayout llRoot;

    @BindView(R.id.peixun_lineclass_rv)
    RecyclerView peixunLineclassRv;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;

    private PeiXunListApi classMuLuApi;
    private PeiXunLineClassRvAdapter videoAdapter;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_pei_xun_line_class;
    }

    @Override
    protected void initView() {

        ButterKnife.bind(this);
        tvTitle.setText("线下课");
        ivRight.setImageResource(R.mipmap.gouwuche_dot);
        videoAdapter = new PeiXunLineClassRvAdapter(context);
        peixunLineclassRv.setLayoutManager(new LinearLayoutManager(context));
        peixunLineclassRv.setAdapter(videoAdapter);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);
        initLineClassData();
    }

    private void initLineClassData() {
        classMuLuApi = new PeiXunListApi(PeiXunLineClassActivity.this, simpleOnNextListener,String.valueOf(2), "0:0", 10, "zh:");
        classMuLuApi.setPageNo(currentPageNumber);
        classMuLuApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(classMuLuApi);
    }

    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(context, subjects.getMsg());
                Logger.e("cjx", subjects.getMsg());
                return;
            }
            PeiXunListBean bean = (PeiXunListBean) subjects.getResult();

            if (isRefresh) {
                videoAdapter.clearData();
            }
            videoAdapter.setDataBeanList(bean.getData());

            if (videoAdapter.getDataBeanList() == null || videoAdapter.getDataBeanList().size() <= 0) {
                loading_layout.setStatus(LoadingLayout.EMPTY);
            } else {
                loading_layout.setStatus(LoadingLayout.SUCCESS);
            }

            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }

        }


        @Override
        public void onError(final Throwable e) {
            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }
            loading_layout.setStatus(LoadingLayout.ERROR);
            Logger.e("cjx", "培训列表数据：" + e.getMessage());
        }
    };




    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

        isRefresh = false;
        currentPageNumber = currentPageNumber + 1;
        classMuLuApi.setPageNo(currentPageNumber);
        HttpManager.getInstance().doHttpDeal(classMuLuApi);

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        isRefresh = true;
        currentPageNumber = 1;
        classMuLuApi.setPageNo(currentPageNumber);
        HttpManager.getInstance().doHttpDeal(classMuLuApi);

    }




    @OnClick(R.id.iv_back)
    public void goBack(View view){
        onBackPressed();
    }

    @OnClick(R.id.iv_right)
    public void goDirect(View view){
        showPopup(PeiXunLineClassActivity.this, llRoot);
    }


    @Override
    protected void addListener() {

    }


    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, PeiXunLineClassActivity.class);
        return intent;
    }
}
